package com.example;

import com.example.bean.ConsumerComponent;
import com.example.util.Consume;
import com.example.util.ReportStatus;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author kangshuai
 */
public class ConsumerApplication {
    public static void main(String[] args) throws Exception {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();


        //输出运行结果 传入 ReportStatus   延迟5秒   间隔5秒   时间单位 秒
        Executors.newSingleThreadScheduledExecutor().scheduleWithFixedDelay(new ReportStatus(), 5, 5, TimeUnit.SECONDS);

        //分别创建是个线程池，不停调用方法A、B、C
        ConsumerComponent service = context.getBean(ConsumerComponent.class);
        Executor executor1 = Executors.newFixedThreadPool(10);
        executor1.execute(new Consume(service, "A"));

        Executor executor2 = Executors.newFixedThreadPool(10);
        executor2.execute(new Consume(service, "B"));

        Executor executor3 = Executors.newFixedThreadPool(10);
        executor3.execute(new Consume(service, "C"));

    }

    @Configuration
    @PropertySource("classpath:/dubbo-consumer.properties")
    @ComponentScan("com.example.bean")
    @EnableDubbo()
    static class ConsumerConfiguration {

    }
}
